package cn.edu.scau.dropship.controller;

import cn.edu.scau.dropship.entity.Goods;
import cn.edu.scau.dropship.entity.Wish_goodsKey;
import cn.edu.scau.dropship.entity.Wish_list;
import cn.edu.scau.dropship.service.GoodsService;
import cn.edu.scau.dropship.service.WishGoodsService;
import cn.edu.scau.dropship.service.WishlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/wishlist")
public class WishlistController {
    @Autowired
    private WishlistService wishlistService;
    @Autowired
    private WishGoodsService wishGoodsService;
    @Autowired
    private GoodsService goodsService;

    @PostMapping(value = "/myWishGoods")
    @ResponseBody
    public List<Goods> myWishGoods(HttpServletRequest request){
        List<Goods> resultlist=new ArrayList<>();

        Integer uid=Integer.valueOf(request.getParameter("uid"));
        //根据uid遍历wishlist，是否已存在心愿单
        Wish_list wish_list=wishlistService.selectByUid(uid);
        if(wish_list==null){
            Wish_list wish_list1=new Wish_list();
            wish_list1.setId(uid);
            wish_list1.setBvoId(uid);
            wishlistService.insert(wish_list1);
        }
        List<Wish_goodsKey> list=wishGoodsService.selectByWid(uid);
        for(int i=0;i<list.size();i++){
            Goods goods=goodsService.selectByPrimaryKey(list.get(i).getGoodsId());
            resultlist.add(goods);
        }
        return resultlist;
    }

    @PostMapping(value = "/deleteGoods")
    @ResponseBody
    public String deleteGoods(HttpServletRequest request){
        Integer uid=Integer.valueOf(request.getParameter("uid"));
        Integer gid=Integer.valueOf(request.getParameter("gid"));
        Wish_goodsKey wish_goodsKey=new Wish_goodsKey();
        wish_goodsKey.setWishId(uid);
        wish_goodsKey.setGoodsId(gid);
        int result=wishGoodsService.deleteByPrimaryKey(wish_goodsKey);
        if(result>0){
            return "删除成功";
        }
        return "删除失败";
    }
}
